package com.bulkmail.sender;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLFile {

	private String from;
	private List<String> to;
	private String subject;
	private String message;
	private String server;
	private String port;
	private String username;
	private String password;
	private String path;

	public WriteXMLFile(DataSession d, String path) {
		from = d.getFrom();
		to =  d.getTo();
		subject = d.getSubject();
		message = d.getMessage();
		server = d.getServer();
		port = d.getPort();
		username = d.getUsername();
		password = d.getPassword();
		this.path = path;
	}

	public void writeXML() {

		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();

			Element beans = doc.createElement("beans");
			doc.appendChild(beans);
			Attr attr = doc.createAttribute("xmlns");
			attr.setValue("http://www.springframework.org/schema/beans");
			beans.setAttributeNode(attr);
			attr = doc.createAttribute("xmlns:xsi");
			attr.setValue("http://www.w3.org/2001/XMLSchema-instance");
			beans.setAttributeNode(attr);
			attr = doc.createAttribute("xsi:schemaLocation");
			attr.setValue("http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd");
			beans.setAttributeNode(attr);

			// staff elements
			Element bean = doc.createElement("bean");
			beans.appendChild(bean);

			// set attribute to staff element
			attr = doc.createAttribute("id");
			attr.setValue("mailSender");
			bean.setAttributeNode(attr);

			attr = doc.createAttribute("class");
			attr.setValue("org.springframework.mail.javamail.JavaMailSenderImpl");
			bean.setAttributeNode(attr);

			Element property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("host");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(server);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("port");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(String.valueOf(port));
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("username");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(username);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("password");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(password);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("javaMailProperties");
			property.setAttributeNode(attr);

			Element props = doc.createElement("props");
			property.appendChild(props);
			Element prop = doc.createElement("prop");
			props.appendChild(prop);
			attr = doc.createAttribute("key");
			attr.setValue("mail.smtp.auth");
			prop.setAttributeNode(attr);
			prop.appendChild(doc.createTextNode("true"));
			property.appendChild(props);
			prop = doc.createElement("prop");
			props.appendChild(prop);
			attr = doc.createAttribute("key");
			attr.setValue("mail.smtp.starttls.enable");
			prop.setAttributeNode(attr);
			prop.appendChild(doc.createTextNode("true"));

			bean = doc.createElement("bean");
			beans.appendChild(bean);
			attr = doc.createAttribute("id");
			attr.setValue("mailMail");
			bean.setAttributeNode(attr);

			attr = doc.createAttribute("class");
			attr.setValue("com.bulkmail.sender.MimeMailMail");
			bean.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("mailSender");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("ref");
			attr.setValue("mailSender");
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("simpleMailMessage");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("ref");
			attr.setValue("customeMailMessage");
			property.setAttributeNode(attr);

			bean = doc.createElement("bean");
			beans.appendChild(bean);
			attr = doc.createAttribute("id");
			attr.setValue("customeMailMessage");
			bean.setAttributeNode(attr);
			attr = doc.createAttribute("class");
			attr.setValue("org.springframework.mail.SimpleMailMessage");
			bean.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("from");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(from);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("to");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			String temp = "";
			for (String s : to) {
				temp +=s+";";
				}
			temp = temp.substring(0,temp.length()-1);
			attr.setValue(temp);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("subject");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(subject);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("text");
			property.setAttributeNode(attr);

			Element value = doc.createElement("value");
			property.appendChild(value);

			value.appendChild(doc.createCDATASection(message));

			// write the content into xml file
			DOMSource source = new DOMSource(doc);
			// (new File("src/main/resources/file.xml")).delete();
			/*
			 * File f = new File("src/main/resources/file.xml"); f.delete();
			 */

			/*
			 * try { Thread.sleep(1000); } catch (InterruptedException e) { //
			 * TODO Auto-generated catch block e.printStackTrace(); }
			 */

			File f = new File(path);
			StreamResult result = new StreamResult(f);

			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			transformer.transform(source, result);
			System.out.println("File saved!");

			// Scanner in = new Scanner(new File(
			// "/home/src/main/resources/file.xml"));

			// while (in.hasNextLine()) {
			// in.nextLine();
			// }
			// in.close();

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
	}
}